package com.huluobo.high3videoproject.adapter;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.bean.ChatEntity;

import java.util.List;

public class ChatMsgAdapter extends BaseQuickAdapter<ChatEntity, BaseViewHolder> {
    public ChatMsgAdapter(@Nullable List<ChatEntity> data) {
        super(R.layout.item_chat_layout, data);
    }

    private Integer[] resId = new Integer[]{R.drawable.chat_bg_0, R.drawable.chat_bg_1, R.drawable.chat_bg_2,
            R.drawable.chat_bg_3, R.drawable.chat_bg_4, R.drawable.chat_bg_3, R.drawable.chat_bg_2,
            R.drawable.chat_bg_1, R.drawable.chat_bg_0};

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, ChatEntity chatEntity) {
        //设置随机背景色
        int index = (int) (Math.random() * 10 % resId.length);
        baseViewHolder.getView(R.id.cl_chat_item).setBackgroundResource(resId[index]);

        //富文本标签 : "李四:发送玫瑰一朵"
        //"<span style = "color:#fff;font-Size:12">李四: </span> <span style="color:#775665">发送玫瑰一朵</span>
        String content = chatEntity.getFromName() + ":" + chatEntity.getMessage();
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        //定义颜色
        ForegroundColorSpan blue = new ForegroundColorSpan(Color.BLUE);
        ForegroundColorSpan yellow = new ForegroundColorSpan(Color.YELLOW);

        //拼接富文本标签字符串
        builder.setSpan(blue, 0, chatEntity.getFromName().length() + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(yellow, chatEntity.getFromName().length() + 1, content.length(),
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        baseViewHolder.setText(R.id.tv_chat_msg_item, builder);
    }
}
